<?php

$loader->requireOnce('includes/SearchingCore/ISearchFilter.class.php');
$loader->requireOnce('includes/LabSearch/LabSearchResult.class.php');

/**
 * Search Filter
 * @package local.includes.SearchingCore
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabSearchFilter implements ISearchFilter {	
	
	const PATH = 'lab_search';
	
	private $logger ;
	private $controller;
	private $currentPath;
	
	function __construct() {		
		$this->logger = LoggerManager::getLogger('modules.dept_laboratory.includes.LabSearch.LabSearchFilter');				
	}
	
	public function setController($controller) {
		$this->controller = $controller;
		$this->currentPath = $this->controller->view->path;  
		$this->controller->view->path = self::PATH;
	}
	
	public function renderSearchFilter() {				
	    $html = $this->controller->view->render("lab_search.html");
	    $this->controller->view->path = $this->currentPath;
	    return $html;	    
	}
	
    public function buildSearchFilterSql($filterData = array()) {
        $this->logger->info('Start buildSearchFilterSql');
        $field =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $sql = array (
            'cols'  => "psn.person_id,
                    psn.name,
                    date_of_birth,
                    pt.patient_code,
                    gender,
                    email,
                    phone_number,
                    address.street, 
                    address.ward_name ward,
                    district.$field district,
                    province.$field province, lab_assignment_ticket_id, enum_status, request_time",
            'from'  => "person psn
                    INNER JOIN patient AS pt ON (psn.person_id = pt.person_id)
                    INNER JOIN lab_assignment_ticket lat ON (lat.patient_id=pt.person_id)
                    LEFT JOIN address ON (psn.address_id = address.address_id)
                    LEFT JOIN province ON (address.province_id = province.province_id)
                    LEFT JOIN district ON (address.district_id = district.district_id)                     
                    ",
            'where' => $this->buildConditionQueryPart($filterData),
            'orderby'=> "psn.name",
            'limit' => 20
            );        
        $this->logger->debug('sql [' . implode(' ', $sql) . ']');
        $this->logger->info('Finish buildSearchFilterSql');
        return $sql;
    }
    
    private function buildConditionQueryPart($filterData = array()){
        $sql = "true";
        if ($filterData['patientCode'] !== "") $sql = $sql. " AND (pt.patient_code LIKE '%".$filterData['patientCode']."%')";
        if ($filterData['patientName'] !== "") $sql = $sql. " AND (psn.name LIKE '%".$filterData['patientName']."%')";
        if ($filterData['gender'] !== "") $sql = $sql. " AND (psn.gender = '".$filterData['gender']."')";
        return $sql;
    }
    
    
    public function buildResultModel($rawResultData = array()) {
        $this->logger->info('Start buildResultModel');
        $labResultModels = array();        
        foreach ($rawResultData as $item) {
            if (!isset($labResultModels[$item['person_id']])) {   
                $labResultModels[$item['person_id']] = new LabSearchResult($item);
            } else {
                $labSearchResultObj = $labResultModels[$item['person_id']];
                $labSearchResultObj->addAssignments($item);
            }
        }
        
        $this->logger->info('Finish buildResultModel');
        return $labResultModels;
    }
    
}
?>